package dobest.ray.com.jiongjiong.activity.action.service;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.dobest.ray.corelibs.utils.ToastMgr;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.List;

import dobest.ray.com.jiongjiong.App;
import dobest.ray.com.jiongjiong.R;
import dobest.ray.com.jiongjiong.activity.BaseActivity;
import dobest.ray.com.jiongjiong.activity.action.ActionPresenter;
import dobest.ray.com.jiongjiong.adapter.ServiceInfoAdapter;
import dobest.ray.com.jiongjiong.adapter.ServiceLabelAdapter;
import dobest.ray.com.jiongjiong.bean.BusinessCircleListBean;
import dobest.ray.com.jiongjiong.bean.PresenterData;
import dobest.ray.com.jiongjiong.callback.PresenterCallBack;
import dobest.ray.com.jiongjiong.views.NavigationBar;

/**
 * Created by Ray on 2016/4/9.
 * https://github.com/ray0807
 * 公共服务或者个体服务
 */
public class ServiceActivity extends BaseActivity<ActionPresenter> implements View.OnClickListener, AdapterView.OnItemClickListener, PresenterCallBack {
    private PullToRefreshListView ptr_list;
    private RelativeLayout rl_most_eye;
    private RelativeLayout rl_most_near;
    private RelativeLayout rl_most_hot;
    private ListView labelListview;
    private ServiceLabelAdapter labelAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_person);
        findViews();
        init();
        addListeners();
    }

    @Override
    public void findViews() {
        setPresenter(new ActionPresenter(this), this);
        navi = (NavigationBar) findViewById(R.id.navi);
        navi.setBackListener(R.mipmap.img_back, "");
        navi.setTitle("个体服务");
        if (App.getInstance().getCity() != null) {
            navi.setTVIVRight(App.getInstance().getCity(), R.color.white, this);
        } else {
            navi.setTVIVRight("选择城市", R.color.white, this);
        }

        ptr_list = (PullToRefreshListView) findViewById(R.id.ptr_list);
        ptr_list.setMode(PullToRefreshBase.Mode.BOTH);
        ptr_list.setAdapter(new ServiceInfoAdapter(this));

        rl_most_eye = (RelativeLayout) findViewById(R.id.rl_most_eye);
        rl_most_near = (RelativeLayout) findViewById(R.id.rl_most_near);
        rl_most_hot = (RelativeLayout) findViewById(R.id.rl_most_hot);

        mPopupView = LayoutInflater.from(this).inflate(R.layout.popupwindow, null);
        labelListview = (ListView) mPopupView.findViewById(R.id.popuwindow_listview);
    }

    @Override
    public void init() {
        labelAdapter = new ServiceLabelAdapter(this);
        labelListview.setAdapter(labelAdapter);
        labelListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                labelAdapter.setSelectItemIndex(arg2);
                ToastMgr.show(labelAdapter.getItem(arg2).toString());
                if (popupwindow != null) {
                    popupwindow.dismiss();
                }
            }

        });
    }

    @Override
    public void addListeners() {
        rl_most_eye.setOnClickListener(this);
        rl_most_near.setOnClickListener(this);
        rl_most_hot.setOnClickListener(this);
        ptr_list.setOnItemClickListener(this);
    }


    private PopupWindow popupwindow;
    private View mPopupView;

    public void showPopuwindow(View clickView) {
        popupwindow = new PopupWindow(mPopupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupwindow.setFocusable(true);
        popupwindow.setOutsideTouchable(true);
        popupwindow.setBackgroundDrawable(new BitmapDrawable());
        // int width = clickView.getWidth();
        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        popupwindow.setWidth(width);
        popupwindow.showAsDropDown(clickView, 0, 0);
        popupwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
            }
        });
    }

    private int flag = -1;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //最热
            case R.id.rl_most_hot:
                showPopuwindow(v);
                clearLabel(2);
                flag = 2;
                getPresenter().getServiceLabels(this, flag);
                break;
            //最近
            case R.id.rl_most_near:
                showPopuwindow(v);
                clearLabel(1);
                flag = 1;
                getPresenter().getServiceLabels(this, flag);
                break;
            //配镜
            case R.id.rl_most_eye:
                showPopuwindow(v);
                clearLabel(0);
                flag = 0;
                getPresenter().getServiceLabels(this, flag);
                break;
            //定位
            case R.id.ll_text_drawable_right:
                Intent it =new Intent(this,SelectCityActivity.class);
                startActivity(it);
                break;
        }

    }

    private void clearLabel(int currentFlag) {
        if (flag != currentFlag) {
            labelAdapter.clearIndex();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent it = new Intent(this, OrganizionDetailsActivity.class);
        startActivity(it);
    }

    @Override
    public <T> void callBackPresenter(PresenterData<T> data) {
        T t = data.data;
        if ("Label".equals(data.tag)) {
            labelAdapter.addData(((List<BusinessCircleListBean>) data.data), flag);
        }
//        if ("coupon_list".equals(data.tag)) {
//            if (isRefresh) {
//                adapter.refreshData(((List<HomePageConponBean>) data.data));
//            } else {
//                adapter.addData(((List<HomePageConponBean>) data.data));
//
//            }
//
//        }
    }
}
